package com.woniuxy.principal.controller;

import com.woniuxy.commons.anno.HasRole;
import com.woniuxy.commons.entity.AllTypeEnroll;
import com.woniuxy.commons.entity.EnrolmentRate;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.service.RemoteAuthService;
import com.woniuxy.principal.service.EnrolmentRateService;
import com.woniuxy.principal.service.SchoolLeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author： 雷鹏
 * Date: 2020/7/16 23:13
 */
@RestController
public class EnrolmentRateController {

    @Autowired
    private EnrolmentRateService enrolmentRateService;

    @Autowired
    private RedisTemplate<String, Object> redisTemlate;

    @Autowired
    private SchoolLeaderService schoolLeaderService;

    @HasRole("principal")
    @GetMapping("/all/{year}")
    public ResponseEntity<AllTypeEnroll> all(@PathVariable("year") String year) {

        AllTypeEnroll allTypeEnroll = enrolmentRateService.getAllTypeEnroll(year);

        ResponseEntity<AllTypeEnroll> responseEntity=new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setMsg("成功");
        responseEntity.setData(allTypeEnroll);

        return responseEntity;
    }

    @HasRole("principal")
    @GetMapping("/five")
    public ResponseEntity<AllTypeEnroll> getFiveEnroll(HttpServletRequest request){

        String refreshToken = request.getHeader("refreshToken");
        Map<Object, Object> data = redisTemlate.opsForHash().entries(refreshToken);
        int rid = (int) data.get("rid");
        //从数据库查出登陆账号所在学校id
        int sid = schoolLeaderService.getSid(rid);

        AllTypeEnroll fiveEnroll = enrolmentRateService.getFiveEnroll(sid);

        ResponseEntity<AllTypeEnroll> responseEntity=new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setMsg("成功");
        responseEntity.setData(fiveEnroll);

        return responseEntity;
    }

}
